package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.CacheConstants;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.vo.LoginVo;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.UserService;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Set;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements UserService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public LoginVo login(LoginDto dto) {
        //1.判断手机号和密码是否传过来
        if(StringUtils.isNotEmpty(dto.getPhone()) && StringUtils.isNotEmpty(dto.getPassword())){
            //2.校验手机号
            ApUser apUser = getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, dto.getPhone()));
            if(apUser == null){
                throw new CustomException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
                //return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }

            //3.加密前端密码
            String frontPassword = dto.getPassword();
            String addFrontSalt = frontPassword + apUser.getSalt();

            //加密
            String frontMd5Password = DigestUtils.md5DigestAsHex(addFrontSalt.getBytes(StandardCharsets.UTF_8));

            //4.对比前端加密后的密码和数据库中的密码
            if (Objects.equals(frontMd5Password,apUser.getPassword())){
                //密码相同
                LoginVo loginVo = new LoginVo();
                LoginVo.UserDTO userDTO = new LoginVo.UserDTO();
                BeanUtils.copyProperties(apUser,userDTO);
                loginVo.setUser(userDTO);
                loginVo.setToken(AppJwtUtil.getToken(Long.valueOf(apUser.getId())));
                return loginVo;
                //return ResponseResult.okResult(loginVo);
            }
            throw new CustomException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            //return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }else{
            //游客登录
            LoginVo loginVo = new LoginVo();
            loginVo.setToken(AppJwtUtil.getToken(0L));
            return loginVo;
            //return ResponseResult.okResult(loginVo);
        }
    }

    @Override
    public void follow(UserRelationDto dto) {
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        //1.确定数据结构 set key
        String userIdStr = user.getId().toString();
        String authorIdStr = dto.getAuthorId().toString();
        String key1 = CacheConstants.FOLLOW_FANS + authorIdStr; //作者角度 粉丝列表
        String key2 = CacheConstants.FOLLOW_FOLLOW + userIdStr; //用户登录角度 关注列表

        if (dto.getOperation() == 0) {
            //2.判断是关注 将数据保存
            stringRedisTemplate.opsForSet().add(key1, userIdStr);
            stringRedisTemplate.opsForSet().add(key2, authorIdStr);
            //互粉
            //Set<String> intersect = stringRedisTemplate.opsForSet().intersect(userIdStr, authorIdStr);
            //查看作者粉丝数量
            //Long size = stringRedisTemplate.opsForSet().size(authorIdStr);
            //查看关注作者数量
            //Long size = stringRedisTemplate.opsForSet().size(userIdStr);
        } else {
            //3.取消关注,将数据删除
            stringRedisTemplate.opsForSet().remove(key1, userIdStr);
            stringRedisTemplate.opsForSet().remove(key1, authorIdStr);
        }
    }
}
